package ru.efko.dims;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SettingsActivity extends Activity {

	private SeekBar vrfSeekBar;
	private SeekBar cmvSeekBar;
	private SeekBar roughSeekBar;
	private SeekBar fromSeekBar;
	private SeekBar vpullSeekBar;
	private SeekBar acoSeekBar;
	private SeekBar accurateSeekBar;
	private SeekBar toSeekBar;

	private EditText vrfEditText;
	private EditText cmvEditText;
	private EditText roughEditText;
	private EditText fromEditText;
	private EditText vpullEditText;
	private EditText acoEditText;
	private EditText accurateEditText;
	private EditText toEditText;

	private final static int SETTINGS_NUMBER = 8;
	private final static int MAX_ALLOWED = 65535;
	private final static int MIN_ALLOWED = 0;

	private Set<SeekBar> seekBarSet = new LinkedHashSet<SeekBar>(
			SETTINGS_NUMBER);
	private Set<EditText> editTextSet = new LinkedHashSet<EditText>(
			SETTINGS_NUMBER);
	
	//TODO bidi map maybe

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		init();
		addListeners();

	}

	private void init() {
		vrfSeekBar = SeekBar.class.cast(findViewById(R.id.vrfVSeekBar));
		seekBarSet.add(vrfSeekBar);
		vrfEditText = EditText.class.cast(findViewById(R.id.vrfVEditText));
		editTextSet.add(vrfEditText);

		cmvSeekBar = SeekBar.class.cast(findViewById(R.id.cmVVSeekBar));
		seekBarSet.add(cmvSeekBar);
		cmvEditText = EditText.class.cast(findViewById(R.id.cmVVEditText));
		editTextSet.add(cmvEditText);

		roughSeekBar = SeekBar.class.cast(findViewById(R.id.roughSeekBar));
		seekBarSet.add(roughSeekBar);
		roughEditText = EditText.class.cast(findViewById(R.id.roughEditText));
		editTextSet.add(roughEditText);

		fromSeekBar = SeekBar.class.cast(findViewById(R.id.fromVSeekBar));
		seekBarSet.add(fromSeekBar);
		fromEditText = EditText.class.cast(findViewById(R.id.fromVEditText));
		editTextSet.add(fromEditText);

		vpullSeekBar = SeekBar.class.cast(findViewById(R.id.vpullSeekBar));
		seekBarSet.add(vpullSeekBar);
		vpullEditText = EditText.class.cast(findViewById(R.id.vpullEditText));
		editTextSet.add(vpullEditText);
		
		acoSeekBar = SeekBar.class.cast(findViewById(R.id.acoSeekBar));
		seekBarSet.add(acoSeekBar);
		acoEditText = EditText.class.cast(findViewById(R.id.acoEditText));
		editTextSet.add(acoEditText);
		
		accurateSeekBar = SeekBar.class.cast(findViewById(R.id.accurateSeekBar));
		seekBarSet.add(accurateSeekBar);
		accurateEditText = EditText.class.cast(findViewById(R.id.accurateEditText));
		editTextSet.add(accurateEditText);
		
		toSeekBar = SeekBar.class.cast(findViewById(R.id.toSeekBar));
		seekBarSet.add(toSeekBar);
		toEditText = EditText.class.cast(findViewById(R.id.toEditText));
		editTextSet.add(toEditText);
	}
	
	//TODO add sender
	
	private void addListeners(){
		Iterator<EditText> etIterator = editTextSet.iterator(); 
		Iterator<SeekBar> sbIterator = seekBarSet.iterator();
		while (sbIterator.hasNext()){
			final SeekBar current = sbIterator.next();
			current.setProgress(0);
			final EditText associated = etIterator.next();
			associated.setText(String.valueOf(MIN_ALLOWED));
			current.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				
				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					String value = String.valueOf(current.getProgress());
					associated.setText(value);
					
				}
			});
			
			associated.addTextChangedListener(new TextWatcher() {
				
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
					Integer value = s.length() == 0 
							? MIN_ALLOWED 
							: Integer.parseInt(s.toString());
					if (value < 0){
						value = MIN_ALLOWED;
						associated.setText(String.valueOf(value));
					}
					if (value > MAX_ALLOWED){
						value = MAX_ALLOWED;
						associated.setText(String.valueOf(value));
					}
					
					current.setProgress(value);
					
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					
				}
			});
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.settings, menu);
		return true;
	}

}
